<?PHP
/**
 * 控制器处理类 test
 */
class Admin_user_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','会员管理');
		$this->assign('menuid1','2'); 
		$this->assign('menuid2','1');
		
		//权限判断
		factory::module('admin')->authSys('user');
	}

 
	
	
	
	
	##################################
	/* 用户管理 */
	################################## 
	function index__(){ 
		 
	 	$this->assign('nav','会员列表');
	 	
		
		
		
	
	}  
	function index_list__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['state']))   $_POST['state']  = '';   
			if(!isset($_POST['classes']))   $_POST['classes']  = '';   
			if(!isset($_POST['sendtype']))   $_POST['sendtype']  = '';   
  	  #查询条件
			$sql_where = ' where a.usertype = 2  and a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.username','a.name','a.phone','a.bdate','a.edate'  ));
			if($_POST['sendtype'] != ''){
				$sql_where .= ' and a.sendtype="'.$_POST['sendtype'].'" ';
			}

			if($_POST['state'] != ''){
				$sql_where .= ' and a.state="'.$_POST['state'].'" ';
			}
			if($_POST['classes'] != ''){
				$sql_where .= ' and a.classes="'.$_POST['classes'].'" ';
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_user` a  left join t_list_user_classes b on a.classes=b.id 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					a.* ,
					b.val as classes
				from `t_user` a    left join t_list_user_classes b on a.classes=b.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 添加 
	public function  add__(){ 
		$this->assign('menuid2','2');
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
		 
			$db = factory::getDBO(); 
			$db->begin();
			# 重名判断
			if($db->loadresult('select count(*) from t_user where deleted=0 and usertype=2 and username="'.$form['username'].'" ')>0){
			
				exit('操作失败！【会员编号】已经存在，请重新填写！');
			}		 
			
		 
			
			//主表
			$form['usertype'] = '2';
			$form['create_date'] = 'now()';
			$form['create_ip'] = func::get_client_ip();
			
			if($form['birthday'] == ''){
				unset($form['birthday']);
			}
			if($form['contractdate'] == ''){
				unset($form['contractdate']);
			}
			$form['balance'] = 0;
			
			$id = $db->insert('user',$form);
			if(!$id){
				$db->rollback();
				exit('系统繁忙，请稍后重试！');
			} 
			$db->commit();
			exit('{success:1,msg:"添加成功！"}');
		} 
		
	 	$this->assign('nav','会员列表 - 添加会员');
	  
		
		
		$db = factory::getDBO(); 
	 	$this->assign('rs_deliver',  $db->loadassoclist('select  * from `t_deliver`    where deleted=0 and status=1   ') ); 

		 
	}
	# 修改 
	public function modify__(){ 
		$this->assign('menuid2','0');
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) )); 
 
			$db = factory::getDBO(); 
			$db->begin();
			if($form['birthday'] == ''){
				unset($form['birthday']);
			}
			if($form['contractdate'] == ''){
				unset($form['contractdate']);
			}
			$newpwd = $form['password'];
			unset($form['password']);
			
			//主表
		  $db->update('user',$form);
			if($newpwd != ''){
				$db->update('user',array(
					'id'=>$form['id'],
					'password'=>$newpwd
				));	
			}
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		} 
		$this->assign('nav','会员列表 - 修改会员信息');  
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_user` a  where deleted=0 and usertype=2 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		$this->assign('rs',$rs);
		
		
		
		$db = factory::getDBO(); 
		$rs_deliver =  $db->loadassoclist('select  * from `t_deliver`    where deleted=0 and ( status=1 or id="'.$rs['deliver_id'].'" )  ');
	 	$this->assign('rs_deliver', $rs_deliver ); 
	}
	/**
	 * 删除
	 */
	public function del__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			exit('暂停删除功能！');
			$id = $_POST['id']; 
			$db = factory::getDBO(); 
			$sql = "update t_user set deleted=1 where usertype=2 and id=".$id ;
			$delete = $db->query($sql);
			if($delete){ 
				exit('{success:1,msg:"操作成功！"}');
			}else{
				exit('系统繁忙，请稍后再试！');
			}
		}	
	}	
	 
 
}
?>